package wusiyuan.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;

import dao.CityDao;
import dao.ProvinceDao;
import dao.UserDao;
import dao.impl.CityDaoImpl;
import dao.impl.CountyDaoImpl;
import dao.impl.ProvinceDaoImpl;
import dao.impl.UserDaoImpl;
import entity.DetailAddress;
import entity.User;
import entity.UserAddress;
import entity.User_Address;
import entity.User_Inf;
import wusiyuan.dao.ShowTheAddressDao;
import wusiyuan.dao.impl.ShowTheAddressDaoImpl;


public class ShowTheAddressServlet extends HttpServlet {
	private ShowTheAddressDao sad =new ShowTheAddressDaoImpl();
	private UserDao ud = new UserDaoImpl();
	private ProvinceDao pd = new ProvinceDaoImpl();
	private CityDao cd = new CityDaoImpl();
	private CountyDaoImpl cdi = new CountyDaoImpl();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");		
		String username = request.getParameter("username");
		System.out.println(username);
		UserAddress address = new UserAddress();		
		if (username!=null&&username!="") {
			User queryUserByName = ud.queryUserByName(username);
			User_Inf user_Inf = ud.queryUserByUsername(username);
			List<User_Address> list = sad.getUserAddressByUserId(user_Inf.getUserId());		
			List<UserAddress> addressList = new ArrayList<UserAddress>();
			for (User_Address user_Address : list) {			
				if (user_Address.getIs_default()!=1) {			
				address.setConsignee(user_Address.getConsignee());
				address.setPhone(user_Address.getPhone());
				address.setSex(user_Address.getSex());
				address.setZip_code(user_Address.getZip_code());
				address.setProvince(pd.getProvinceByProId(user_Address.getProvince()).getName());
				address.setCity(cd.getCityByCityId(user_Address.getCity()).getName());
				address.setDistrict(cdi.getCountyByCountyId(user_Address.getDistrict()).getName());
				address.setAddress(user_Address.getAddress());
				address.setRemark(user_Address.getRemark());
				address.setIs_default(user_Address.getIs_default());
				addressList.add(address);
				address = new UserAddress();
				}		
			}
			for (User_Address user_Address : list) {		
				if (user_Address.getIs_default()==1) {
					address.setConsignee(user_Address.getConsignee());
					address.setPhone(user_Address.getPhone());
					address.setSex(user_Address.getSex());
					address.setZip_code(user_Address.getZip_code());
					address.setProvince(pd.getProvinceByProId(user_Address.getProvince()).getName());
					address.setCity(cd.getCityByCityId(user_Address.getCity()).getName());
					address.setDistrict(cdi.getCountyByCountyId(user_Address.getDistrict()).getName());
					address.setAddress(user_Address.getAddress());
					address.setRemark(user_Address.getRemark());
					address.setIs_default(user_Address.getIs_default());
					addressList.add(address);
				}
				address = new UserAddress();
			}
		
			for (UserAddress UserAddress : addressList) {
				System.out.println(UserAddress);
			}
			response.getWriter().print(JSON.toJSONString(addressList));
		}
		
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
